﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;
using WebBanThuoc.Server;
using System.IO;

namespace WebBanThuoc
{
    public partial class HoiDap : System.Web.UI.Page
    {
        IServerWeb sw = new ServerWeb();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) loadData();
            this.LoadComplete += new EventHandler(HoiDap_LoadComplete);  
        }
        void HoiDap_LoadComplete(object sender, EventArgs e)
        {
            sw.closeConnection();
        }
        public void loadData()
        {
            int trang = 1;
            int soBaiTrenTrang = 5;

            if (Request.Params["trang"] != null)
            {
                trang = Convert.ToInt32(Function.Func.beginConvertDataType(Request.Params["trang"], Function.pDataType.Integer));
            }
            BaiDang_DAO daoBD = new BaiDang_DAO(sw.Connection);
            List<BaiDang> listBD = daoBD.getList_Paging_All(trang, soBaiTrenTrang);
            int faq_tongSoTrang = daoBD.totalNumberOfPages_All(soBaiTrenTrang);
            Session.Add("listBD",listBD);
            Session.Add("faq_tongSoTrang", faq_tongSoTrang);
            // TraLoiBaiDang_DAO daoTL = new TraLoiBaiDang_DAO(sw.Connection);
            //List<TraLoiBaiDang> listTL = daoTL.getAll();
           // Session.Add("listTL", listTL);
        }
        public static string StripHtml(string html, bool allowHarmlessTags)
        {
            if (html == null || html == string.Empty)
                return string.Empty;

            if (allowHarmlessTags)
                return System.Text.RegularExpressions.Regex.Replace(html, "", string.Empty);

            return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*(>|$)'", string.Empty);
        }

        protected void faq_Submit_Click(object sender, EventArgs e)
        {
           
            faq_ccJoin.ValidateCaptcha(faq_sCode.Text);
            if (!faq_ccJoin.UserValidated)
            {
                faq_thongbao.Text = "Mã bảo vệ chưa chính xác, vui lòng nhập lại.";
                return;
            }
        
            BaiDang bd = new BaiDang();
            BaiDang_DAO daoBD = new BaiDang_DAO(sw.Connection);
            bd.MaBaiDang = daoBD.createMaBaiDang();
          
            bd.TenNguoiHoi = StripHtml(faq_ten.Text, false);
            bd.Email = StripHtml(faq_email.Text, false);
            bd.SoDienThoai = StripHtml(faq_phone.Text, false);
            bd.NgayDang = DateTime.Now;
            bd.SoLanTraLoi = 0;
            bd.SoLuotXem = 0;
            bd.MaDanhMuc = "DMDD1";
            bd.NoiDung = Request.Params[faq_noidung.ClientID];// StripHtml(faq_noidung.Value, false);
            if (bd.NoiDung == null)
            {
                bd.NoiDung = Request.Params[faq_noidung.ClientID.Replace('_', '$').Replace("faq$noidung", "faq_noidung")];
            }
            bd.NoiDung = Function.Convert_Txt_HTML_TH.ToHtml(bd.NoiDung);//HttpUtility.HtmlEncode(bd.NoiDung);
            StringWriter myWriter = new StringWriter();
            bd.KiemDuyet = false;
            bd.IDKhachHang = 0;
            bd.TenCauHoi = StripHtml(faq_chude.Text, false);
            bd.TraLoi = "";
            int okthemBD = daoBD.add(bd);
           // Session.Add("bd", bd);
            //okthemBD = 1;
            if (okthemBD > 0)
            {
                String url = "HoiDapConfirm.aspx";
                //Session.Remove("bd");
                Response.Redirect(url);
             }
            else
                ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "alert('Gửi phản hồi không thành công!');", true);
        }

        
    }
}